<?php
class PostsController extends PostsAppController {

	var $name = 'Posts';
	var $uses = array('Posts.Post', 'Posts.Category');
	
	var $helpers = array('Html', 'Form', 'Text', 'Time','Extend.Textile','Extend.GershImage', 'Extend.Lightbox');
	
	var $components = array('RequestHandler');
	var $paginate = array(
		'limit' => 10,
		'order' => array('Post.date' => 'desc'),
		'conditions' => array('Post.published' => 1)
	);
	
	function index($cat = null) {
		
		//$this->Portfolio->recursive = 1;
		if($cat == null) {
			$posts = $this->paginate();
		} else {
			$params = array(
				'contain' => false,
				'fields' => array('DISTINCT (Post.id) as id'),
				'conditions' => $this->paginate['conditions'],
				'joins' => array()
			);
			$joins1 = array(
				'table' => $this->Post->tablePrefix.'posts_categories_posts',
				'alias' => 'CategoriesPost',
				'type' => 'inner',
				'foreignKey' => false,
				'conditions'=> array('CategoriesPost.post_id = Post.id')
			);
			$joins2 = array(
				'table' => $this->Post->tablePrefix.'posts_categories',
				'alias' => 'Category',
				'type' => 'inner',
				'foreignKey' => false,
				'conditions'=> array(
				'Category.id = CategoriesPost.category_id',
				'Category.slug' => $cat
				)
			);
			array_push($params['joins'], $joins1);
			array_push($params['joins'], $joins2);
			
			// query
			$bars = array();
			foreach ($this->Post->find('all', $params) as $row) {
				array_push($bars, $row['Post']['id']);
			}	

			// do basic pagination; id must appear in list of ids from previous query
			$posts = $this->paginate(null, array('Post.id' => $bars));


		}
		$this->set('posts', $posts);
	}
	
	function archive() {
		$params = array(
			'conditions' => $this->paginate['conditions'],
			'recursive' => 1, //int
			'fields' => array('Post.title', 'Post.slug', 'Post.date', 'Post.id'),
			'order' => $this->paginate['order'], 
			'limit' => null
		);
		$posts = $this->Post->find('all', $params);
		$this->set('posts', $posts);
		$this->pageTitle = __('Archived posts', true);
	}
	
	function category($num = null) {
		
		//if(!is_numeric($num)) $num = 5;
		$this->Post->Category->Behaviors->attach('Containable');
		$this->Post->Category->contain('Post.id');
		$params = array(
			'fields' => array('Category.id','Category.title','Category.slug'), 
			'order' => array('Category.title' => 'asc'),
			'limit' => $num
		);
		
		$categories = $this->Post->Category->find('all', $params);
		if(isset($this->params['requested'])) { 
			return $categories; 
		}
		$this->set('categories', $categories);
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Post.', true));
			$this->redirect(array('action'=>'index'));
		}
		if(is_numeric($id)) {
			$post = $this->Post->read(null, $id);
		} else {
			$post = $this->Post->findBySlug($id);
		}
		$this->pageTitle = $post['Post']['title'] . " / " . $this->name;
		// Update view count
		$this->Post->id = $post['Post']['id'];
		$current_views = $post['Post']['views']  = $post['Post']['views'] + 1;
		$this->Post->saveField('views', $current_views);
		$this->set('post', $post);
	}
	
	function recentposts($num = null) {
		//$this->pageTitle = $num . ' ' . __('recent posts', false);
		$params = array(
			'conditions' => $this->paginate['conditions'],
			'recursive' => -1, //int
			'fields' => array('Post.title', 'Post.slug', 'Post.date'),
			'order' => $this->paginate['order'], 
			'limit' => $num
		);
		$posts = $this->Post->find('all', $params);
		if(isset($this->params['requested'])) { 
			return $posts; 
		}
		$this->set('posts', $posts);
	}
	
	function feed($var = null) {
		Configure::write ('debug', 0);
		$this->RequestHandler->setContent('rss', 'application/xml');
		
		$this->pageTitle = __('RSS feed of posts', true);
		$params = array(
			'conditions' => $this->paginate['conditions'],
			'recursive' => 1, //int
			'order' => $this->paginate['order'],
			'limit' => Configure::read('Posts.rss_limit')
		);
		
		$posts = $this->Post->find('all', $params);
		$this->set('channel', array(
			'title' => Configure::read('Posts.rss_title'),
			'link' => Configure::read('Posts.site_url'),
			'description' => __('RSS of latest posts', true),
			'language' => Configure::read('Posts.language')));
		$this->set('posts', $posts);
		
	}


	function admin_index() {
		$this->Post->recursive = 1;
		$this->paginate['conditions'] = null;
		$this->set('posts', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Post.', true));
			$this->redirect('/admin/posts');
		}
		$this->set('post', $this->Post->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {

			$this->Post->create();
			if ($this->Post->save($this->data)) {
				$this->Session->setFlash(__('The Post has been saved', true));
				$this->redirect('/admin/posts');
			} else {
				$this->Session->setFlash(__('The Post could not be saved. Please, try again.', true));
			}
		}
		
		$categories = $this->Post->Category->find('list');
		$this->set(compact('categories'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Post', true));
			$this->redirect('/admin/posts');
		}
		if (!empty($this->data)) {
			if ($this->Post->save($this->data)) {
				$this->Session->setFlash(__('The Post has been saved', true));
				//$this->redirect('/admin/posts');
			} else {
				$this->Session->setFlash(__('The Post could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Post->read(null, $id);
		}
		$categories = $this->Post->Category->find('list');
		//$images = $this->Post->Image->find('list');
		$this->set(compact('categories'));
		$this->set('post', $this->Post->read(null, $id));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Post', true));
			$this->redirect('/admin/posts');
		}
		if ($this->Post->del($id)) {
			$this->Session->setFlash(__('Post deleted', true));
			$this->redirect('/admin/posts');
		} else {
			$this->Session->setFlash(__('Post soft deleted', true));
			$this->redirect('/admin/posts');
		}
	}

}
?>